package com.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.entity.Phone;
import com.shop.entity.PhoneAttr;
import com.shop.entity.Res;
import com.shop.service.ColorService;
import com.shop.service.InfoService;
import com.shop.service.PhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-04
 */
@RestController
@RequestMapping("/phone")
public class PhoneController {

    @Autowired
    PhoneService phoneService;
    @Autowired
    ColorService colorService;
    @Autowired
    InfoService infoService;

    @Value("${imgdir}")
    private String UPLOAD_DIR;


    @GetMapping()
    public Res get () {
        List<Phone> list = phoneService.list();
        return Res.success(list);
    }

    @PostMapping("search")
    public Res search(@RequestBody PhoneAttr phone) {
        List<Phone> list = phoneService.queryByAttr(phone);

        return Res.success(list);
    }

    @PostMapping("count")
    public Res count (@RequestBody PhoneAttr phoneAttr) {
       Integer count = phoneService.countByAttr(phoneAttr);
       return Res.success(count);
    }
    @PostMapping("save")
    public Res save (@RequestBody Phone phone) {
        //TODO
        phone.setColor(colorService.getById(phone.getColorId()).getColor());
        phone.setBrandName(infoService.getById(phone.getBrandId()).getMsg());
        phone.setTypeName(infoService.getById(phone.getTypeId()).getMsg());
        phone.setCreateTime(new Date());
        phone.setId(UUID.randomUUID().toString());
        phoneService.save(phone);
        return Res.success();
    }
    @GetMapping("getMemory")
    public Res listMemory() {
        List<Integer> collect = phoneService.getPhoneMemory();
        return Res.success(collect);
    }

    @GetMapping("getDisk")
    public Res listDisk() {
        List<Integer> collect = phoneService.getPhoneDisk();
        return Res.success(collect);
    }

    @PostMapping("IMGupload")
    public Res upload(MultipartFile file) throws IOException {

        byte[] bytes = new byte[1024];
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(i);
        UUID prefix = UUID.randomUUID();
        String fileName = prefix + suffix;

        FileOutputStream fileOutputStream = new FileOutputStream(UPLOAD_DIR + fileName);

        InputStream inputStream = file.getInputStream();

        while (true){
            int read = inputStream.read(bytes);
            fileOutputStream.write(bytes,0,read);
            if (read < bytes.length){
                break;
            }
        }
        return Res.success("http://localhost:8848/img/"+ fileName);
    }

    @GetMapping("getOne")
    public Res getOne(String  id) {
        Phone one = phoneService.getById(id);
        return Res.success(one);
    }

    @GetMapping("pageList")
    public Res pageList(Integer start,Integer offset) {
        List<Phone> list = phoneService.list();
        List<Phone> subList = list.subList(start >= list.size() ? list.size() : start, Math.min(offset + start, list.size()));
        return Res.success(subList);
    }
    @GetMapping("count")
    public Res count() {
        return Res.success(phoneService.count());
    }

    @PostMapping("upOrDown")
    public Res upOrDown(@RequestBody HashMap<String ,String> params) {
        String id = params.get("id");
        Phone one = phoneService.getById(id);
        one.setIsDelete(one.getIsDelete().equals(PhoneService.DEL)?PhoneService.NOT_DEL:PhoneService.DEL);
        boolean update = phoneService.updateById(one);
        return update?Res.success() : Res.error();
    }
    @PostMapping("change")
    public Res change(@RequestBody Phone phone) {
        boolean update = phoneService.updateById(phone);
        return update?Res.success():Res.error();
    }
}

